﻿using System.Collections.Specialized;

namespace LBSoft.Common.DotNetExt
{
    /// <summary>
    /// 键值对扩展类，主要应用于AppSettings
    /// </summary>
    public static class ExtNameValueCollection
    {
        /// <summary>
        /// 根据键获取值，如果获取失败，则返回string.Empty
        /// </summary>
        /// <param name="source">表示可通过键或索引访问的关联 System.String 键和 System.String 值的集合。</param>
        /// <param name="name">键值</param>
        /// <returns></returns>
        public static string GetValue(this NameValueCollection source, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                 return string.Empty;
            }
            return source[name] ?? string.Empty;
        }

        /// <summary>
        /// 根据索引获取值，如果获取失败，则返回string.Empty
        /// </summary>
        /// <param name="source">表示可通过键或索引访问的关联 System.String 键和 System.String 值的集合。</param>
        /// <param name="name">索引值</param>
        /// <returns></returns>
        public static string GetValue(this NameValueCollection source, int index)
        {
            if (index < 0 || index >= source.Count)
            {
                return string.Empty;
            }
            return source[index] ?? string.Empty;
        }

    }
}
